﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using System.Text;

public partial class giaovien_TrangBangDiemMonHoc : System.Web.UI.Page
{
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsLoadCombobox cls = new clsLoadCombobox();
    clsKiemTraTrangThai clstt = new clsKiemTraTrangThai();
    clsThaoTac thaotac = new clsThaoTac();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Username"] == null || Session["Username"].ToString() == "")
        {
            Response.Redirect("~/GiaoDienClient/TrangChu.aspx");
        }
        else
        {
            if (!IsPostBack)
            {
                string magv = Session["Username"].ToString();
                cls.LoadComboboxNamHoc(drNamHoc);
                cls.LoadComboboxLopHoc(drLopHoc, int.Parse(drNamHoc.SelectedValue.ToString()));
                cls.LoadComboboxMonHocTheoMaGV(drMonHoc, int.Parse(drLopHoc.SelectedValue.ToString()), int.Parse(drNamHoc.SelectedValue.ToString()),magv);

            }
        }

    }
    void HienThiBangDiemMonHoc(int manam, int maky,int malop,int mamon)
    {
        grvNhapDiem.DataSource = db.LayDanhSachHocSinhTheoLop(mamon, maky, manam, malop);
        grvNhapDiem.DataBind();
    }
    protected void btnHienThi_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        HienThiBangDiemMonHoc(manam,maky,malop,mamon);
        lblTrangThai.Text = clstt.trangthai(manam, malop, mamon, maky);
        if (lblTrangThai.Text == "Đã duyệt")
        {
            btnGuiBangDiem.Visible = false;
        }
        if (lblTrangThai.Text != "Đã duyệt")
        {
            btnGuiBangDiem.Visible = true;
        }

    }
    void ExporttuDatabase(string fileName)
    {
        Response.ContentType = "application/csv";
        Response.Charset = "";
        Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
        Response.ContentEncoding = Encoding.Unicode;
        Response.BinaryWrite(Encoding.Unicode.GetPreamble());
        //DataTable dtb = getTable();
        // Response.Write(dtb.ToString());
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        if (rdoKyI.Checked == true)
        { maky = 1; }
        if (rdoKyII.Checked == true)
        { maky = 2; }
        try
        {
            StringBuilder sb = new StringBuilder();
            string hocky = "I";
            if (rdoKyII.Checked == true)
            { hocky = "II"; }
            sb.Append("BẢNG ĐIỂM MÔN"+" "+drMonHoc.SelectedItem.Text.ToUpper()+"HỌC KỲ"+" "+hocky+" "+"NĂM HỌC" + " " + drNamHoc.SelectedItem.Text+"\t"+"LỚP"+"  "+drLopHoc.Text);
            Response.Write(sb.ToString() + "\n");
            Response.Flush();
           

            sb = new StringBuilder();
            sb.Append("STT" + "\t" + "MÃ HS" + "\t" + "HỌ VÀ TÊN" + "\t" + "M1" + "\t" + "M2" + "\t" + "M3" + "\t" + "M4" + "\t" + "M5" + "\t" + " 15'1" + "\t" + " 15'2" + "\t" + " 15'3" + "\t" + " 15'4" + "\t" + " 15'5" + "\t" + "45'1" + "\t" + "45'2" + "\t" + "45'3" + "\t" + "45'4"  + "\t" + "HK" + "\t" + "Điểm TB");
            Response.Write(sb.ToString() + "\n");
            Response.Flush();
            //Duyệt từng bản ghi 
           var c = db.LayDanhSachHocSinhTheoLop(mamon, maky, manam, malop);
            int soDem = 0;
            foreach (var con in c)
            {
                sb = new StringBuilder();
                sb.Append(soDem+1);
                sb.Append("\t");
                if (con.MaHS != null)
                {
                    sb.Append(con.MaHS);
                    sb.Append("\t");
                }
                if (con.MaHS == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.TenHS != null)
                {
                    sb.Append(con.TenHS);
                    sb.Append("\t");
                }
                if (con.TenHS == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.DiemMieng1 != null)
                {
                    sb.Append(con.DiemMieng1.ToString());
                    sb.Append("\t");
                }
                if (con.DiemMieng1 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.DiemMieng2 != null)
                {
                    sb.Append(con.DiemMieng2.ToString());
                    sb.Append("\t");
                }
                if (con.DiemMieng2 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.DiemMieng3 != null)
                {
                    sb.Append(con.DiemMieng3.ToString());
                    sb.Append("\t");
                }
                if (con.DiemMieng3 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.DiemMieng4 != null)
                {
                    sb.Append(con.DiemMieng4.ToString());
                    sb.Append("\t");
                }
                if (con.DiemMieng4 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.DiemMieng5 != null)
                {
                    sb.Append(con.DiemMieng5.ToString());
                    sb.Append("\t");
                }
                if (con.DiemMieng5 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                //------------------------------------//
                if (con.Diem15_1 != null)
                {
                    sb.Append(con.Diem15_1.ToString());
                    sb.Append("\t");
                }
                if (con.Diem15_1 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.Diem15_2 != null)
                {
                    sb.Append(con.Diem15_2.ToString());
                    sb.Append("\t");
                }
                if (con.Diem15_2 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.Diem15_3 != null)
                {
                    sb.Append(con.Diem15_3.ToString());
                    sb.Append("\t");
                }
                if (con.Diem15_3 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.Diem15_4 != null)
                {
                    sb.Append(con.Diem15_4.ToString());
                    sb.Append("\t");
                }
                if (con.Diem15_4 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.Diem15_5 != null)
                {
                    sb.Append(con.Diem15_5.ToString());
                    sb.Append("\t");
                }
                if (con.Diem15_5 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                //---------------------------------------//
                if (con.Diem45_1 != null)
                {
                    sb.Append(con.Diem45_1.ToString());
                    sb.Append("\t");
                }
                if (con.Diem45_1 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.Diem45_2 != null)
                {
                    sb.Append(con.Diem45_2.ToString());
                    sb.Append("\t");
                }
                if (con.Diem45_2 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.Diem45_3 != null)
                {
                    sb.Append(con.Diem45_3.ToString());
                    sb.Append("\t");
                }
                if (con.Diem45_3 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.Diem45_4 != null)
                {
                    sb.Append(con.Diem45_4.ToString());
                    sb.Append("\t");
                }
                if (con.Diem45_4 == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                sb.Append("\t");
                if (con.DiemThi != null)
                {
                    sb.Append(con.DiemThi.ToString());
                    sb.Append("\t");
                }
                if (con.DiemThi == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                if (con.DTBMonHocKy != null)
                {
                    sb.Append(con.DTBMonHocKy.ToString());
                    sb.Append("\t");
                }
                if (con.DTBMonHocKy == null)
                {
                    sb.Append("");
                    sb.Append("\t");
                }
                Response.Write(sb.ToString() + "\n");
                Response.Flush();
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        //dtb.Dispose();
        Response.End();

    }
   
    protected void btnXuatExcel_Click(object sender, EventArgs e)
    {
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 0;
        string hocky="";
        if (rdoKyI.Checked == true)
        { maky = 1;hocky="I"; }
        if (rdoKyII.Checked == true)
        { maky = 2;hocky="II"; }
       // ExporttuDatabase("Môn_" + drMonHoc.Text + "_Lop(" + drLopHoc.Text + ").xls");
        //DataTable c = db.funcLayDiemHSTheoLop(mamon, maky, manam, malop);
        DataTable dt = thaotac.ToDataTable(db,db.funcLayDiemHSTheoLop(manam,maky,manam,malop));

        thaotac.Export(dt, "Môn " + drMonHoc.SelectedItem.Text, "BẢNG ĐIỂM MÔN " + drMonHoc.SelectedItem.Text+"  "+"   HỌC KỲ "+hocky+" NĂM HỌC: "+drNamHoc.SelectedItem.Text+" LỚP HỌC: "+drLopHoc.SelectedItem.Text);
    }
    

    protected void btnGuiBangDiem_Click(object sender, EventArgs e)
    {
        int malop = int.Parse(drLopHoc.SelectedValue.ToString());
        int manam = int.Parse(drNamHoc.SelectedValue.ToString());
        int mamon = int.Parse(drMonHoc.SelectedValue.ToString());
        int maky = 1;
        if (rdoKyII.Checked == true)
        { maky = 2; }

        if (clstt.kiemtra(manam, malop, mamon, maky) == false)
        {

            TrangThaiBangDiem ttbd = new TrangThaiBangDiem();
            ttbd.MaNam = manam;
            ttbd.MaMon = mamon;
            ttbd.MaLop = malop;
            ttbd.MaKy = maky;
            ttbd.TrangThaiChoDuyet = true;
            ttbd.TrangThaiHoanThanh = false;
            ttbd.ChuaSua = false;
           // ttbd.DangSua = false;
            ttbd.DaSua = false;
            db.TrangThaiBangDiems.InsertOnSubmit(ttbd);
            db.SubmitChanges();
            lblTrangThai.Text = "Đang chờ duyệt";


        }
        if (clstt.kiemtra(manam, malop, mamon, maky) == true)
        {
            if (clstt.TTHoanThanh(manam, malop, mamon, maky) == false)
            {
                TrangThaiBangDiem ttbd = db.TrangThaiBangDiems.SingleOrDefault(p => p.MaKy == maky && p.MaLop == malop && p.MaNam == manam && p.MaMon == mamon);
                ttbd.TrangThaiChoDuyet = true;
                ttbd.TrangThaiHoanThanh = false;
                
                db.SubmitChanges();
            }
            lblTrangThai.Text = "Đang chờ duyệt";
        }
    }
    protected void grvNhapDiem_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
